﻿using System;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ink;
using System.Diagnostics;
using System.Runtime.Serialization.Json;
using System.IO;
using Kindohm.KSynth.Library;
using FluxJpeg;
using FluxJpeg.Core;

namespace Kindohm.KSynth.UI
{
    public partial class WaveFormEditor : UserControl
    {
        WaveFormInfo currentWaveForm;
        bool dirty;

        public WaveFormEditor()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WaveFormEditor_Loaded);
            this.surface.Changed += new EventHandler(surface_Changed);
        }

        void WaveFormEditor_Loaded(object sender, RoutedEventArgs e)
        {
            ObservableCollection<WaveFormInfo> items = WaveFormRepository.GetCustomWaveForms();
            this.waveformList.ItemsSource = items;
            this.RegisterNew();
        }

        void surface_Changed(object sender, EventArgs e)
        {
            this.dirty = true;
            this.saveButton.IsEnabled = true;
        }

        void RegisterNew()
        {
            this.dirty = true;
            this.saveButton.IsEnabled = true;

            this.currentWaveForm = new WaveFormInfo();
            this.surface.Reset();
        }

        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            this.RegisterNew();
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.nameBox.Text))
            {
                MessageBox.Show("Name cannot be empty.");
            }
            else
            {
                this.currentWaveForm.Name = this.nameBox.Text;

                CustomWaveForm customWave = this.surface.GetCustomWaveformData();
                customWave.Name = this.currentWaveForm.Name;
                this.currentWaveForm.WaveForm = customWave;
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    WaveFormRepository.WriteCustomWaveForm(file, customWave, 
                        this.surface.GetWaveImage());
                }
                this.dirty = false;
                this.saveButton.IsEnabled = false;
            }
        }

        private void closeButton_Click(object sender, RoutedEventArgs e)
        {


            if (Closed != null)
                this.Closed(this, EventArgs.Empty);
        }
        public event EventHandler Closed;

        private void waveformList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
